package at.fpmedv.eclipse.helma.logging;

import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.IConsoleConstants;

import at.fpmedv.eclipse.helma.console.HelmaConsoleFactory;
import at.fpmedv.eclipse.helma.core.IHelmaEventListener;

/**
 * Listens to server started and opens the helma logging view
 * 
 */
public class HelmaCoreEventListener implements IHelmaEventListener {

	/**
	 * does nothing
	 */
	public HelmaCoreEventListener() {
	}

	/**
	 * does nothing
	 */
	public void errorStarting() {
	}

	/**
	 * does nothing
	 */
	public void errorStopping() {
	}

	/**
	 * open the helma logging view if we find a console view
	 */
	public void serverStarted() {
		// open Helma Logging Console view
        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        if (window != null) {
        	IViewReference[] views = window.getPages()[0].getViewReferences();
            if (views != null) {
            	for (int i = 0; i  < views.length; i++) {
            		// "org.eclipse.ui.console.ConsoleView"
            		String viewId = views[i].getId();
            		if (IConsoleConstants.ID_CONSOLE_VIEW.equals(viewId)) {
            			// open Helma logging
            			HelmaConsoleFactory.showConsole();
            		}
            	}
            }
        }
	}

	/**
	 * does nothing
	 */
	public void serverStoped() {
	}

}
